<?php

class Action
{
    private $per_page = 10;

    public function index(){
        $page_no  = $search_filter = $is_excel = null;

        extract(URLTracker::init(array(
            'page_no' => 1,
            'search_filter' => array(),
            'is_excel' => 0,
        )));
        $relation = array();
        $relation['date !='] = "";
        if (isset($search_filter['start_date']) && $search_filter['start_date']) {
            $relation['date >='] = $search_filter['start_date'];
        }

        if (isset($search_filter['end_date']) && $search_filter['end_date']) {
            $relation['date <='] = $search_filter['end_date'];
        }

        $relation_count = _model('admin_app_order_device_bulletin_day')->getTotal($relation);
        if($relation_count){
            $pager = new Pager($this->per_page);
            if ($pager->generate($relation_count)) {
                Response::assign('pager', $pager);
            }
            $device_by = ' ORDER BY `id` DESC ';
            if ($is_excel != 1) {
                $device_by .= $pager->getLimit($page_no);
            }

            $relation_info = _model('admin_app_order_device_bulletin_day')->getList($relation, $device_by);
            foreach($relation_info as $k => $v){
                $relation_info[$k]['partner_dealer_data'] = json_decode($v['partner_dealer_data'], true);
                $relation_info[$k]['manager_dealer_data'] = json_decode($v['manager_dealer_data'], true);
            }
            if ($is_excel == 1) {
                $export_data = bulletin_helper::decorator_app_bulletin_export($relation_info);
                $this->export_excel($export_data, 'APP简报信息');
            }
            Response::assign('count', $relation_count);
        }
        if (empty($relation_info)) {
            $relation_info = array();
        }

        Response::assign('relation_info', $relation_info);
        Response::assign('search_filter', $search_filter);

        Response::display('admin/app_bulletin.html');
    }

    /**
     * APP简报信息导出
     */
    public function export_excel($list = array(), $filename) {
        list($excelObj, $excelWriter) = _widget('tools.excel')->getExcelObj();

        $cell_title_index = array('A', 'H');
        $cell_index = array('A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K');

        $title = array_values(bulletin_config::$app_bulletin_title);
        $column = array_values(bulletin_config::$app_bulletin_column);

        //设置title (一级标题)
        foreach ($cell_title_index as $i => $cell_title) {
            $excelObj->setCellValue($cell_title.'1', $title[$i]);
        }
        //设置column (二级标题)
        foreach ($cell_index as $k => $v) {
            $excelObj->setCellValue($v.'2', $column[$k]);
            $excelObj->getStyle($v)->getNumberFormat()->setFormatCode("@");
            $excelObj->getStyle($v)->getAlignment()->setWrapText(true);
            if ($k == 0) {
                $excelObj->getColumnDimension()->setWidth(15);
            } else {
                $excelObj->getColumnDimension()->setWidth(12);
            }
        }
        //填充数据
        $index = 3;
        foreach ($list as $key => $value) {
            $data = array_values($value);
            foreach ($cell_index as $cell_key => $cell) {
                $excelObj->setCellValue($cell.$index, $data[$cell_key]);
            }
            $index++;
        }
        $filename = date('Y-m-d') . $filename;
        return _widget('tools.excel')->download($excelWriter, $filename);
    }
}
